package com.xxgc.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OrderRabbitConfig {

    // 交换机名称
    private final String EXCHANGE_NAME = "order_exchange";

    // 队列名称
    private final String QUEUE_NAME = "order_queue";

    //死信交换机名称
    private final String DEAD_EXCHANGE_NAME = "dead_exchange_name";

    //死信队列名称
    private final String DEAD_QUEUE_NAME = "dead_queue_name";


    /**
     * 创建正常交换机(手机)
     * */
    @Bean("orderExchange")
    public Exchange getExchange(){
        return ExchangeBuilder
                .topicExchange(EXCHANGE_NAME)
                .durable(true) // 是否持久化
                .build();
    }

    /**
     * 创建正常队列(讨论的事情)
     * */
    @Bean("orderQueue")
    public Queue getMessageQueue(){
        return QueueBuilder
                .durable(QUEUE_NAME)
                .ttl(30000)//30s没有支付则订单过期
                .deadLetterExchange(DEAD_EXCHANGE_NAME)//如果没有支付就把这条数据移交到死信队列里面
                .deadLetterRoutingKey(DEAD_QUEUE_NAME)//订单过期的死信交换机
                .build();
    }

    /**
     * 创建死信交换机(手机)
     * */
    @Bean("deadExchange")
    public Exchange getDeadExchange(){
        return ExchangeBuilder
                .topicExchange(DEAD_EXCHANGE_NAME)
                .durable(true) // 是否持久化
                .build();
    }

    /**
     * 创建死信队列(讨论的事情)
     * */
    @Bean("deadQueue")
    public Queue getDeadQueue(){
        return QueueBuilder
                .durable(DEAD_QUEUE_NAME)
                .build();
    }


    /**
     * 正常交换机和正常队列的绑定（手机和事情）
     * */
    @Bean
    public Binding bindingOrderExchangeQueue(
            @Qualifier("orderExchange") Exchange exchange, // @Qualifier
            @Qualifier("orderQueue") Queue queue){
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("#.order.#")// 审判长梗
                .noargs();
    }

    /**
     * 死信交换机和死信队列的绑定（手机和事情）
     * */
    @Bean
    public Binding bindingDeadExchangeQueue(
            @Qualifier("deadExchange") Exchange exchange, // @Qualifier
            @Qualifier("deadQueue") Queue queue){
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("#.order_expire.#")// 订单过期
                .noargs();
    }



}
